
//
//  AppDelegate.swift
//  weibo
//
//  Created by 阿城 on 15/12/5.
//  Copyright © 2015年 阿城. All rights reserved.
//

import UIKit
@UIApplicationMain
class AppDelegate: UIResponder, UIApplicationDelegate {

    var window: UIWindow?

    var obj :NSObjectProtocol?
    
    
    func application(application: UIApplication, didFinishLaunchingWithOptions launchOptions: [NSObject: AnyObject]?) -> Bool {
        
        SCStatusDAL.clearDBCache()
        
//        printLog(NSBundle.mainBundle().infoDictionary)
//        let ss = SCEmoticonViewModel.shareEmoticons.contentEmoticon
//        let a = ss[1][0]
//        printLog(ss)
        
        window = UIWindow(frame: UIScreen.mainScreen().bounds)
        
//        window?.rootViewController = SCNewfeatureViewController()
        
        window?.makeKeyAndVisible()
        
        
        if !SCUserInfoViewModel.shareInfoViewModel.isLogOn {
            window?.rootViewController = SCTabBarViewController()
        }else if isNewVersion() {
            window?.rootViewController = SCNewfeatureViewController()
        }else{
            window?.rootViewController = SCWelcomeViewController()
        }
   
        obj = NSNotificationCenter.defaultCenter().addObserverForName(kSwitchVcNotifcation, object: nil, queue: nil) { [weak self](noti) -> Void in
            
            //let vc = noti.object
            
            if noti.object is SCWebViewController {
                
                self?.window?.rootViewController = SCWelcomeViewController()
            }else{
                self?.window?.rootViewController = SCTabBarViewController()
            }
        }
        
        return true
    }

    
    private func isNewVersion () -> Bool{
        
        let lacolVersion = NSUserDefaults.standardUserDefaults().stringForKey("lacolVersion")
        
        let currentVersion = NSBundle.mainBundle().infoDictionary!["CFBundleShortVersionString"]! as! String
        
        NSUserDefaults.standardUserDefaults().setObject(currentVersion, forKey: "lacolVersion")
        
        if let local = lacolVersion where currentVersion.compare(local) != NSComparisonResult.OrderedDescending {
            return false
        }
        
        return true
    }
    
    deinit{
        NSNotificationCenter.defaultCenter().removeObserver(obj!)
    }
    
}

